package com.sample.springbatch.job.sample.processor;

import com.sample.springbatch.model.ActionLog;
import com.sample.springbatch.model.RepayOrderInfo;
import com.sample.springbatch.service.IActionLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

/**
 * Created by zwg on 2017/7/20.
 */
public class RepayOrderProcessor implements ItemProcessor<RepayOrderInfo,RepayOrderInfo> {

    @Autowired
    IActionLogService actionLogService;

    private static final Logger logger = LoggerFactory.getLogger(RepayOrderProcessor.class);

    @Override
    public RepayOrderInfo process(final RepayOrderInfo order) throws Exception {
        ActionLog log = new ActionLog();
        log.setContent(order.toString());
        log.setStatus(1);
        log.setCreateTime(new Date());
        log.setUpdateTime(new Date());
        log = actionLogService.save(log);
        //数据进行封装
        RepayOrderInfo repayOrderInfo = new RepayOrderInfo();
        BeanUtils.copyProperties(order,repayOrderInfo);
        repayOrderInfo.setBussDate(new Date());
        //logger.info("repayOrder :{}",order);
        //Thread.sleep(1000L);

        //跳过数据
        if("90000000001".equals(repayOrderInfo.getUserId())){
            //throw new MySkipException("skip data");
            throw new RuntimeException("中断退出，测试ActionLog是否会回滚");
        }
        log.setStatus(2);
        log.setUpdateTime(new Date());
        log = actionLogService.save(log);

        return repayOrderInfo;
    }
}
